﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Xml.Linq;
using System.Windows;
using  Acando.Dynamics.CRM.Silverlight.Library.Controls.FieldControls;
using Acando.Dynamics.CRM.Silverlight.Library.Helper;
using Acando.Dynamics.CRM.Silverlight.Library.Interfaces;
using Acando.Dynamics.CRM.Silverlight.Library.MetaSdk;
using Acando.Dynamics.CRM.Silverlight.Library.CrmSdk;

namespace Acando.Dynamics.CRM.Silverlight.Library.Controls.FieldControls
{
    public class ValueControlFactory
    {
        private ValueControlFactory() { }

        public static FrameworkElement CreateControl(IRecordControl mainControl, EntityMetadata metadata, XElement cellDef)
        {
            var fieldName = cellDef.Elements("control").First().Attribute("id").Value;
            return CreateControl(mainControl, metadata, fieldName);
        }

        public static FrameworkElement CreateControl(IRecordControl mainControl, EntityMetadata metadata, string fieldName)
        {            
            
            string controlType;

            //Catch special types
            if (fieldName == "notescontrol")
                return new CrmNotesControl(mainControl, fieldName, fieldName);
            else
                controlType = metadata.GetAttributeType(fieldName).Value.ToString().ToLower();

            //AttributeMetadata attrMeta = GetAttributeMetadata(metadata, fieldName);

            AttributeMetadata attrMeta = metadata.GetAttributeMetadata(fieldName);

            if (controlType == "string")
                return new CrmStringControl(mainControl, controlType, fieldName);
            else if (controlType == "picklist")
                return new CrmPicklistControl(mainControl, metadata,  fieldName, controlType, DropdownType.Picklist);
            else if (controlType == "status")
                return new CrmPicklistControl(mainControl, metadata, fieldName, controlType, DropdownType.StatusReason);
            else if (controlType == "state")
                return new CrmStateInfoControl(mainControl, controlType, fieldName);
            else if (controlType == "owner" || controlType == "customer" || controlType == "lookup")
                return new CrmLookupControl(mainControl, 
                                            controlType, 
                                            fieldName, 
                                            attrMeta as LookupAttributeMetadata, 
                                            ((IProviedMetaDataLayer)mainControl).Metadatalayer);
            else if (controlType == "boolean")
                return new CrmBoolControl(mainControl, controlType, fieldName);
            else if (controlType == "money")
                return new CrmMoneyControl(mainControl, controlType, fieldName);
            else if (controlType == "integer")
                return new CrmNumberControl(mainControl, controlType, fieldName,0);
            else if (controlType == "float")
                return new CrmNumberControl(mainControl, controlType, fieldName, 1);
            else if (controlType == "memo")
                return new CrmMemoControl(mainControl, controlType, fieldName);
            else if (controlType == "datetime")
                return new CrmDateTimeControl(mainControl, controlType, fieldName, (DateTimeAttributeMetadata) attrMeta);
            else if (controlType == "partylist")
                return new CrmPartylistControl(mainControl, controlType, fieldName);
            else
            {
                TextBlock t = new TextBlock();
                t.Text = "Not known type :" + controlType;
                return t;
            }
        }

        private static AttributeMetadata GetAttributeMetadata(EntityMetadata metadata, string fieldName)
        {
            foreach (var attr in metadata.Attributes)
                if (attr.LogicalName == fieldName)
                    return attr;

            throw new Exception("Attribute metadata not found for " + fieldName);
        }
    }
}
